const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
const path = require('path')
const webpack = require("webpack");

let cesiumSource = './node_modules/cesium/Source'
let cesiumWorkers = '../Build/Cesium/Workers'

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true, //执行打包操作之前,清空dist目录
  },
  resolve: {
    alias: {
        '@': path.resolve('src'),
        cesium: path.resolve(__dirname, cesiumSource),
    },
  },
  devtool: 'inline-source-map', //追踪开发中的错误
  devServer: {
    static: './dist', //告知webpack从dist目录下加载文件
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
    ],
  },
  // optimization: {
  //   runtimeChunk: 'single',//一个页面使用多个入口时,需要配置此项
  // },
  plugins: [
    new HtmlWebpackPlugin({
      title: '主页',
    }),
    new NodePolyfillPlugin(),
    new CopyPlugin({
      patterns: [
        { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers'},
        { from: path.join(cesiumSource, 'Assets'), to: 'Assets' },
        { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' },
        {  from: path.join(cesiumSource, 'ThirdParty/Workers'),to: 'ThirdParty/Workers', },
      ],
    }),
    new webpack.DefinePlugin({
      CESIUM_BASE_URL: JSON.stringify('./'),
    }),
  ],
}
